package com.market.sys.api.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.market.sys.common.Result;
import com.market.sys.dto.BoothSearchDto;
import com.market.sys.dto.CommentSearchDto;
import com.market.sys.entity.Booth;
import com.market.sys.entity.Comment;
import com.market.sys.entity.User;
import com.market.sys.mapper.BoothMapper;
import com.market.sys.service.IBoothService;
import com.market.sys.vo.BoothReportVO;
import com.market.sys.vo.BoothVO;
import io.netty.util.internal.UnstableApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Struct;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/admin/booth")
public class AdminBoothController {

    @Autowired
    private IBoothService boothService;

    @Resource
    private BoothMapper boothMapper;

    @GetMapping("/report")
    public Result<?> report(){
        List<BoothReportVO> list = boothMapper.report();
        return Result.success(list);
    }

    @PostMapping("/save")
    public Result<?> save(@RequestBody Booth booth){
        if (Objects.isNull(booth)){
            return Result.error("提交信息不能为空！");
        }
        boolean result = boothService.saveOrUpdate(booth);
        if (result){
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    @PostMapping("/list")
    public Result<Map<String ,Object>> search(@RequestBody BoothSearchDto form){
        MPJLambdaWrapper<Booth> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Booth.class);
        wrapper.leftJoin(User.class, User::getId, Booth::getUserId);
        if (form.getState() != null){
            wrapper.eq(Booth::getState, form.getState());
        }

        wrapper.selectAs(User::getNickName, Booth::getNickName);

        Page<Booth> page = boothService.page(new Page<>(form.getPageNo(), form.getPageSize()), wrapper);
        List<Booth> boothList = page.getRecords();
        List<BoothVO> records = new ArrayList<>();
        for (Booth booth : boothList) {
            BoothVO boothVO = new BoothVO();
            BeanUtil.copyProperties(booth, boothVO);
            String coverImages = booth.getCoverImages();
            if (StrUtil.isNotEmpty(coverImages)){
                String[] split = coverImages.split(",");
                List<String> coverImagesList = new ArrayList<>(split.length);
                for (String s : split) {
                    coverImagesList.add(s);
                }
                boothVO.setCoverImages(coverImagesList);
            }
            records.add(boothVO);
        }

        Map<String,Object> data = new HashMap<>();

        data.put("total",page.getTotal());
        data.put("rows",records);

        return Result.success(data);
    }
}
